﻿using CoreProject.DTO;
using CoreProject.IService;
using CoreProject.Model;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreProject.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PderController : ControllerBase
    {
        private readonly IPderService _derService;
        private readonly IPderxqService _derxqService;
        public PderController(IPderxqService pderxqService,IPderService pderService)
        {
            _derService = pderService;
            _derxqService = pderxqService;
        }
        // GET: api/<PderController>
        [HttpGet]
        [Route("Pdermain")]
        public async Task<ResultData<List<PderDto>>> Get()
        {
            var dd = await _derService.cha();
            var dd1 = await _derxqService.cha();
            dd1.Sum(n => n.Price);
            var tt = from p in dd
                     select new PderDto
                     {
                         Pbh = p.Pbh,
                         Dbh = p.Dbh,
                         dizhi = p.Dizhi,
                         UserID = p.UserId,
                         Pdata = p.Pdata,
                         Wcdata = p.Wcdata,
                         Zhifu = p.Zhifu,
                         Ptype = p.Ptype,
                         Sdizhi = p.Sdizhi,
                         zt = p.Zt,
                         Phone = p.User.Phone,
                         UserName = p.User.UserName,

                         Price = (float)dd1.Where(n => n.Pbh == p.Pbh).Sum(n1 => n1.Price * n1.Shu),
                         pxq = from n in p.Pderxqs
                               select new PderxqDto
                               {
                                   Pbh = n.Pbh,
                                   Pxqid = n.Pxqid,
                                   Price = (float)n.Price,
                                   Gid = n.GId,
                                   Name=n.GIdNavigation.Name,
                                   Images=n.GIdNavigation.Images,
                                   Shu = (int)n.Shu,
                                   Pl = n.Pl,
                                   Sfpl = n.Sfpl
                               }
                     };
            ResultData<List<PderDto>> result = new ResultData<List<PderDto>>();
            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.data = tt.ToList();
            return result;
        }
        [HttpGet("MonthStatistics")]
        public async Task<ResultData<List<MonthStatistics1Dto>>> GetMonthStatistics(string nian)
        {
            ResultData<List<MonthStatistics1Dto>> result = new ResultData<List<MonthStatistics1Dto>>();

            var list = await _derService.GetMonthStatistics(nian);
            var zz = new List<MonthStatistics1Dto>();

            foreach (var item in list)
            {
                MonthStatistics1Dto dd = new MonthStatistics1Dto();
                dd.MonthNumber = item.MonthNumber;
                dd.TotalAmount = item.TotalAmount;
                dd.ShuAmount = item.ShuAmount;
                dd.JinAmount = item.JinAmount;
                zz.Add(dd);
            }

            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.data = zz;
            return result;
        }



        [HttpGet]
        [Route("Pderfen")]
        public async Task<ResultPageList<PderDto>> Get(int page, int limit, DateTime sj, DateTime sj1,string name="")
        {
            int count = 0;

            
            var dd = await _derService.chaf(page, limit, name, sj, sj1);
            var dd1 = await _derxqService.cha();
            dd1.Sum(n => n.Price);
            var tt = from p in dd.list
                     select new PderDto
                     {
                         Pbh = p.Pbh,
                         Dbh = p.Dbh,
                         dizhi = p.Dizhi,
                         UserID = p.UserId,
                         Pdata = p.Pdata,
                         Wcdata = p.Wcdata,
                         Zhifu = p.Zhifu,
                         Ptype = p.Ptype,
                         Sdizhi = p.Sdizhi,
                         zt = p.Zt,
                         Phone = p.User.Phone,
                         UserName = p.User.UserName,

                         Price = (float)dd1.Where(n => n.Pbh == p.Pbh).Sum(n1 => n1.Price * n1.Shu),
                         pxq = from n in p.Pderxqs
                               select new PderxqDto
                               {
                                   Pbh = n.Pbh,
                                   Pxqid = n.Pxqid,
                                   Price = (float)n.Price,
                                   Gid = n.GId,
                                   Name = n.GIdNavigation.Name,
                                   Images = n.GIdNavigation.Images,
                                   Shu = (int)n.Shu,
                                   Pl = n.Pl,
                                   Sfpl = n.Sfpl
                               }
                     };

            ResultPageList<PderDto> result = new ResultPageList<PderDto>();
            result.state = 200;
            result.code = "1";
            result.page = page;
            result.size = limit;
            result.count = dd.count;
            result.msg = "ok";
            result.data = tt.ToList();
            return result;

        }

        
       

        // GET api/<PderController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<PderController>
        [HttpPost]
        public async Task<ResultHandle> Post([FromBody] predaddDto value)
        {

            Pder pr = new Pder();
            pr.Pbh = value.Pbh;
            pr.Pdata = DateTime.Now;
            pr.Dizhi = value.dizhi;
            pr.Zhifu = value.Zhifu;
            pr.Dbh =value.Dbh;
            pr.UserId = value.UserID;
            pr.Ptype = value.Ptype;
            pr.Wcdata = DateTime.Now;
            pr.Sdizhi = value.Sdizhi;
            pr.Zt = 1;
            var zz = await _derService.AddAsync(pr);
            pr.Pderxqs = new List<Pderxq>(){ };
            foreach (predaddyDto py in value.predkey)
            {
                Pderxq key = new Pderxq()
                {
                    Pxqid = 0,
                    Pbh = pr.Pbh,
                    Price = py.Price,
                    GId = py.Gid,
                    Shu = py.Shu,
                    Sfpl = py.Sfpl,
                    Pl = py.Pl
                };
                await _derxqService.AddAsync(key);
            }
           
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = zz ? "1" : "-1";
            result.msg = zz ? "添加成功!" : "添加失败!";
            return result;
        }
        // POST api/<PderController>
        [HttpPost("jia")]
        public async Task<ResultHandle> Posttt([FromBody] predaddDto value)
        {

            Pder pr = new Pder();
            pr.Pbh = value.Pbh;
            pr.Pdata = DateTime.Now;
            pr.Dizhi = value.dizhi;
            pr.Zhifu = value.Zhifu;
            pr.Dbh =value.Dbh;
            pr.UserId = value.UserID;
            pr.Ptype = value.Ptype;
            pr.Wcdata = DateTime.Now;
            pr.Sdizhi = value.Sdizhi;
            pr.Zt = 1;
            var zz = await _derService.AddAsync(pr);
            pr.Pderxqs = new List<Pderxq>() { };
            foreach (predaddyDto py in value.predkey)
            {
                Pderxq key = new Pderxq()
                {
                    Pxqid = 0,
                    Pbh = pr.Pbh,
                    Price = py.Price,
                    GId = py.Gid,
                    Shu = py.Shu,
                    Sfpl = py.Sfpl,
                    Pl = py.Pl
                };
                await _derxqService.AddAsync(key);
            }

            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = zz ? "1" : "-1";
            result.msg = zz ? "添加成功!" : "添加失败!";
            return result;
        }

        [HttpPut]
        [Route("gai")]
        public async Task<ResultHandle> gai([FromBody] gaiya value)
        {
            Pder dr= new Pder();
            dr.Pbh=value.Pbh;
            dr.Zt = value.Zt;
                       
          var bol= await _derService.gai1(dr);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol? "修改成功!" : "修改失败!";
            return result;
        }

        // PUT api/<PderController>/5
        [HttpPut]
        public async Task<ResultHandle> Put([FromBody] predaddDto value)
        {
            Pder pr = new Pder();
           // pr.Pbh = value.Pbh;
            pr.Pdata = value.Pdata;
            pr.Dizhi = value.dizhi;
            pr.Zhifu = value.Zhifu;
            pr.Dbh = value.Dbh;
            pr.UserId = value.UserID;
            pr.Zt = 1;
            pr.Pderxqs = new List<Pderxq>() { };
            foreach (predaddyDto py in value.predkey)
            {
                Pderxq key = new Pderxq()
                {
                    Pxqid = 0,
                    Pbh = pr.Pbh,
                    Price = py.Price,
                    GId = py.Gid,
                    Shu = py.Shu,

                };
                pr.Pderxqs.Add(key);
            }
            bool zz = await _derService.gai(pr);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = zz ? "1" : "-1";
            result.msg = zz ? "修改成功!" : "修改失败!";
            return result;
        }

        // DELETE api/<PderController>/5
        [HttpDelete]
        public async Task<ResultHandle> Delete(string id)
        {
            var zz = await _derService.shan(id);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = zz ? "1" : "-1";
            result.msg = zz ? "删除成功!" : "删除失败!";
            return result;
        }
    }
}
